# +----------------------------------------------------------------------------+
# | Copyright (C)2001-2006 Altera Corporation                                  |
# |  Any megafunction design, and related net list (encrypted or decrypted),   |
# |  support information, device programming or simulation file, and any other |
# |  associated documentation or information provided by Altera or a partner   |
# |  under Altera's Megafunction Partnership Program may be used only to       |
# |  program PLD devices (but not masked PLD devices) from Altera.  Any other  |
# |  use of such megafunction design, net list, support information, device    |
# |  programming or simulation file, or any other related documentation or     |
# |  information is prohibited for any other purpose, including, but not       |
# |  limited to modification, reverse engineering, de-compiling, or use with   |
# |  any other silicon devices, unless such use is explicitly licensed under   |
# |  a separate agreement with Altera or a megafunction partner.  Title to     |
# |  the intellectual property, including patents, copyrights, trademarks,     |
# |  trade secrets, or maskworks, embodied in any such megafunction design,    |
# |  net list, support information, device programming or simulation file, or  |
# |  any other related documentation or information provided by Altera or a    |
# |  megafunction partner, remains with Altera, the megafunction partner, or   |
# |  their respective licensors.  No other licenses, including any licenses    |
# |  needed under any third party's intellectual property, are provided herein.|
# |  Copying or modifying any file, or portion thereof, to which this notice   |
# |  is attached violates this copyright.                                      |
# +----------------------------------------------------------------------------+




# +----------------------------------------------------------------------------+
# | file: UP_PTF_Parser.pm                                                     |
# |                                                                            |
# | This module is provided by                                                 |
# | Altera's University Program.                                               |
# |                                                                            |
# | Its purpose is to extract necessary                                        |
# | information from an IP cores and the                                       |
# | system's PTF files so that cores can                                       |
# | be customized for the user's needs.                                        |
# |                                                                            |
# | version: 0.5                                                               |
# |                                                                            |
# +----------------------------------------------------------------------------+

package UP_PTF_Parser;
use Exporter;


@ISA = Exporter;
@EXPORT = qw(
	initialize_ptf_parser
	get_num_hdl_files
	get_hdl_filename
	check_top_level_module_flag

	check_parameter_from_ptf_file
	check_section_from_ptf_file

	get_addition_files
);
#	parse_hdl_filenames_from_ptf_file
#	parse_parameters_from_ptf_file
#	parse_sections_from_ptf_file


use strict;
use format_conversion_utils;
use ptf_parse;
use UP_System_Info;


my @hdl_files;
my $parameters;
my $sections;

sub initialize_ptf_parser
{
	my $error;

	($error) = parse_hdl_filenames_from_ptf_file ();
	return $error if ($error != 1);

	($error) = parse_parameters_from_ptf_file ();
	return $error if ($error != 1);

	($error) = parse_sections_from_ptf_file ();
	return $error if ($error != 1);

	return 1;
}

sub get_num_hdl_files
{
	return ($#hdl_files + 1);
}

sub get_hdl_filename
{
	my ($file_num) = (@_);

	return $hdl_files[$file_num]{filename};
}

sub check_top_level_module_flag
{
	my ($file_num) = (@_);

	return $hdl_files[$file_num]{top_level_module};
}

sub parse_hdl_filenames_from_ptf_file
{
	my $module_ptf			= get_module_ptf ();

	my $top_level_module	= get_data_by_path($module_ptf,"CLASS/CB_GENERATOR/top_module_name");

	$top_level_module		=~ /:/;
	$top_level_module		= $`;

	my $hdl_files_ptf		= get_child_by_path($module_ptf, "CLASS/CB_GENERATOR/HDL_FILES");
    my $child_count			= get_child_count($hdl_files_ptf);
	
	for(my $i = 0; $i < $child_count; $i++)
	{
		my $hdl_file_ptf = get_child($hdl_files_ptf,$i);
		if(get_name($hdl_file_ptf) eq "FILE")
		{
			my %hdl_file_info->{filename}		= get_data_by_path($hdl_file_ptf,"filepath");
			if ( %hdl_file_info->{filename} eq ("hdl/".$top_level_module))
			{
				%hdl_file_info->{top_level_module} = 1;
			}
			else
			{
				%hdl_file_info->{top_level_module} = 0;
			}

			push @hdl_files, \%hdl_file_info;
		}
	}

	return 1;
}

sub parse_parameters_from_ptf_file
{
	my $new_module_ptf	= get_module_ptf_from_system_ptf();

	my $parameter_ptf	= get_child_by_path($new_module_ptf, "WIZARD_SCRIPT_ARGUMENTS/PARAMETERS");
    my $parameter_count	= get_child_count($parameter_ptf);

	for(my $i = 0; $i < $parameter_count; $i++)
	{
        my $a_parameter		= get_child($parameter_ptf,$i);

        my $parameter_name	= get_name($a_parameter);
        my $value			= get_data($a_parameter);

		$parameters->{$parameter_name} = $value;
	}

	return 1;
}

sub check_parameter_from_ptf_file
{
	my ($parameter_name) = (@_);

	if (defined ($parameters->{$parameter_name}))
	{
		return 1, $parameters->{$parameter_name};
	}
	
	return "Parameter not found!";
}

sub parse_sections_from_ptf_file
{
	my $new_module_ptf	= get_module_ptf_from_system_ptf();

	my $section_ptf		= get_child_by_path($new_module_ptf, "WIZARD_SCRIPT_ARGUMENTS/SECTIONS");
    my $section_count	= get_child_count($section_ptf);

	for(my $i = 0; $i < $section_count; $i++)
	{
        my $a_section		= get_child($section_ptf,$i);

        my $section_name	= get_name($a_section);
        my $value			= get_data($a_section);

		$sections->{$section_name} = $value;
	}

	return 1;
}

sub check_section_from_ptf_file
{
	my ($section_name) = (@_);

	if (defined ($sections->{$section_name}))
	{
		return 1, $sections->{$section_name};
	}
	
	return "Section not found!";
}

sub get_addition_files
{
	my @addition_files;
	
	my $module_ptf			= get_module_ptf ();
	my $addition_files_ptf	= get_child_by_path($module_ptf, "CLASS/CB_GENERATOR/OTHER_FILES");
    my $child_count			= get_child_count($addition_files_ptf);
	
	for(my $i = 0; $i < $child_count; $i++)
	{
		my $addition_file_ptf = get_child($addition_files_ptf,$i);

		if(get_name($addition_file_ptf) eq "FILE")
		{
			push @addition_files, get_data_by_path($addition_file_ptf,"filepath");
		}		
	}

	return 1, @addition_files;
}

# End with success

return 1;

# end of file

